var BatchTasker = {
    isFF : (navigator.userAgent.toLowerCase().indexOf("firefox")> 0),
    isIE : (navigator.userAgent.toLowerCase().indexOf('msie') > 0),
    isChrome : (navigator.userAgent.toLowerCase().indexOf('chrome') > 0),
    Agent: null,
    SetThunder: function () {
        this.initialize();
    },
    BeginBatch: function () {
        if (!this.Agent) {
            this.initialize();
        }
        if (!this.Agent) {
            alert("您的浏览器不支持迅雷现在");
        }
        if (this.isIE) {
            this.Agent.clearTasks();
            this.Agent.setThunder5()
        } else {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            this.Agent.BeginBatch();
        }
    },
    AddTask: function (txtUrl, urlName) {
        urlName = !urlName ? txtUrl : urlName;
        if (!this.Agent) this.initialize();
        if (this.isIE) {
            this.Agent.AddBatchTask(txtUrl, urlName);
        } else {
            alert("您的浏览器不支持迅雷现在");
        }
    },
    EndBatch: function () {
        if (this.isIE) {
            this.Agent.download();
        } else {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            try {
                this.Agent.EndBatch();
            } catch (e) {
                alert(e)
            }
        }
    },
    initialize: function () {
        this.Agent = null;
        if (this.isIE) {
            this.Agent = thunderBatchTasker.getInstance();
        } else {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                var tmp = Components.classes["@xunlei.com/ThunderLoader;1"].createInstance();
                this.Agent = tmp.QueryInterface(Components.interfaces.IThunderDownload);
            } catch (err) {
                this.Agent = null;
                return false;
            }
        }
        return true;
    }
};

var thunderBatchTasker = {
    tasks: [],
    clearTasks: function () {
        thunderBatchTasker.tasks.length = 0;
    },
    setThunder5: function () {
        thunderBatchTasker.getInstance(false);
    },
    AddBatchTask: function () {
        var arg1 = arguments[0];
        var arg2 = arguments[1];
        if (!arg1) {
            return;
        }
        arg2 = (!arg2) ? arg1 : arg2;
        thunderBatchTasker.tasks[thunderBatchTasker.tasks.length] = new Array(arg1, arg2);
    },
    download: function () {
        var pid = arguments[0];
        if (!this.thunderObj) {
            thunderBatchTasker.getInstance();
        }
        for (var i = 0; i < thunderBatchTasker.tasks.length; i++) {
            this.thunderObj.AddTask4(thunderBatchTasker.tasks[i][0], "", "", thunderBatchTasker.tasks[i][1], document.location.href.replace(/#/g, ''), -1, 0, -1, document.cookie, "", "");
        }
        this.thunderObj.CommitTasks2(1);
        return true;
    },
    getInstance: function () {
        this.thunderObj = thunderBatchTasker.getThunder5();
        return this;
    },
    getThunder5: function () {
        try {
            return new ActiveXObject("ThunderAgent.Agent.1");
        } catch (e) {
            return false;
        }
    }
};